fix: handle invalid JSON responses in CouchDB handlers - #11263
Open
mtaha435 wants to merge 1 commit into
Open
Conversation
Member
|
Hi @mtaha435, thanks for taking the time to contribute! Before we can review this PR, we'd ask that you open an issue first describing the problem you're aiming to solve. This helps our maintainer team triage and confirm the approach before code review, and saves everyone time if adjustments are needed. |
mtaha435
force-pushed
the
fix/handle-invalid-json-couchdb
branch
from
July 28, 2026 20:33
b551ceb to
39e7c91
Compare
mtaha435
force-pushed
the
fix/handle-invalid-json-couchdb
branch
from
July 28, 2026 20:47
39e7c91 to
07d4738
Compare
Author
|
Opened issue #11304 as requested and linked it to this PR! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixed #11304
The API contains unhandled
JSON.parse()errors in stream event handlers that process CouchDB responses. When an upstream server (CouchDB or proxy) returns non-JSON content—such as HTML error pages, corrupted data, or truncated responses—theJSON.parse()call throws an unhandledSyntaxError.Since these event handlers are not part of the Express middleware error chain, the error is not caught. This results in:
Root Cause
Two locations parse CouchDB responses without try-catch protection:
api/src/controllers/infodoc.js:16— Records successful document writesapi/src/routing.js:1132— Main request/response interceptor for offline filteringWhen does this occur?
While these scenarios are not typical, they can occur in production and cause request failures without meaningful error messages.
Solution
Wrap
JSON.parse()in try-catch blocks at both locations to gracefully handle parse errors:api/src/controllers/infodoc.js:api/src/routing.js:Changes
File:
api/src/controllers/infodoc.jsJSON.parse()in try-catchFile:
api/src/routing.jsJSON.parse()in try-catchparsedBodyfor clarityTesting
Unit Tests Added: 15 test cases in:
api/tests/mocha/controllers/infodoc.spec.js(new file)api/tests/mocha/routing.spec.js(extended)Test Coverage:
Tests Passed: 3 error-handling tests confirmed fix works
Manual Validation:
Impact
Benefits:
Backwards Compatibility:
Risk Assessment:
Files Modified
Total diff: ~558 lines (includes comprehensive tests)